# -*- coding: cp936 -*-




import pygame
from pygame.locals import *

pygame.init()
width, height = 480, 480
screen=pygame.display.set_mode((width, height))
black, red, blue, white = (0,0,0), (255,0,0), (0,0,255), (255,255,255)
empty = 0
xxx = -1
ooo = 1

def draw_game():
          pygame.draw.rect(screen, white, (0, 0, width, height))

          pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
          pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
          pygame.draw.line(screen, black, (0, 160), (480, 160), 5)
          pygame.draw.line(screen, black, (0, 320), (480, 320), 5)

          for row, line in enumerate(state):                          #row, line => 0,[0,0,0]  1,[0,0,0]  2,[0,0,0]


                    for col, val in enumerate(line):                  #col, val => 0,0  1,0  2,0


                              if val == xxx:
                                        # Draw an X as two crossing lines.




                
                                        upper_left = (col * 160 + 5, row * 160 + 5)
                                        lower_right = (col * 160 + 155, row * 160 + 155)
                                        pygame.draw.line(screen, red, upper_left, lower_right, 5)
                
                                        upper_right = (col * 160 + 155, row * 160 + 5)
                                        lower_left = (col * 160 + 5, row * 160 + 155)
                                        pygame.draw.line(screen, red, upper_right, lower_left, 5)
                              elif val == ooo:
                                        rect = (col * 160 + 5, row * 160 + 5, 150, 150)
                                        pygame.draw.ellipse(screen, blue, rect, 5)
         
          
          pygame.display.flip()


def begin():
          global state
          state = [[empty] * 3,[empty] * 3,[empty] * 3]
          draw_game()
          pygame.display.flip()
begin()

while True:
          draw_game()

          pygame.display.flip()
          
          event  =  pygame.event.wait()
              
          pos = None


          if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
          elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                    draw_game()
                    pygame.display.flip()
                    pos = (event.pos[1] / 160, event.pos[0] / 160)
                    row, col = pos
                    state[row][col] = -1

